package com.luhuiguo.cms.server.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.luhuiguo.cms.server.service.MapperService;

@Service
public class MapperServiceImpl implements MapperService {

    @Autowired
    private Mapper mapper;

    public <T> T map(Object source, Class<T> destinationClass) {
        if (null == source) {
            return null;
        }
        return mapper.map(source, destinationClass);
    }

    public void map(Object source, Object destination) {
        if (null == source) {
            return;
        }
        mapper.map(source, destination);

    }

    public <T> T map(Object source, Class<T> destinationClass, String mapId) {
        if (null == source) {
            return null;
        }
        return mapper.map(source, destinationClass, mapId);
    }

    public void map(Object source, Object destination, String mapId) {
        if (null == source) {
            return;
        }
        mapper.map(source, destination, mapId);
    }

    public <T> List<T> mapList(List<?> source, Class<T> destinationClass) {
        if (null == source) {
            return null;
        }
        List<T> list = new ArrayList<T>();
        for (Object obj : source) {
            list.add(mapper.map(obj, destinationClass));
        }
        return list;
    }

    public <T> List<T> mapList(List<?> source, Class<T> destinationClass, String mapId) {
        if (null == source) {
            return null;
        }
        List<T> list = new ArrayList<T>();
        for (Object obj : source) {
            list.add(mapper.map(obj, destinationClass, mapId));
        }
        return list;
    }

}
